class code2 {
    //在排序数组中查找元素的第一个和最后一个位置
    public int[] searchRange(int[] nums, int target) {
        int n = nums.length;
        //空数组
        if(n == 0) {
            return new int[]{-1,-1};
        }

        int[] ret = new int[2];
        int left = 0,right = n-1;

        //找左边界
        while(left < right) {
            int mid = left + (right-left)/2;
            if(nums[mid] >= target) {
                right = mid;
            } else {
                left = mid+1;
            }
        }
        //没找到
        if(nums[left] != target) {
            return new int[]{-1,-1};
        }
        ret[0] = left;

        right = n-1;
        //找右边界
        while(left < right) {
            int mid = left + (right-left+1)/2;
            if(nums[mid] > target) {
                right = mid-1;
            } else {
                left = mid;
            }
        }
        ret[1] = left;

        return ret;
    }
} 